package com.solcoo.common.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * 
 * @author panjunlin
 * 
 * @version 创建时间：2012-9-28 下午4:53:08
 * 
 */
public class CollectionUtil {
	public static boolean isEmpty(Collection<?> coll) {
		return coll == null || coll.size() == 0;
	}

	public static int getLength(Collection<?> coll) {
		return isEmpty(coll) ? 0 : coll.size();
	}

	public static boolean isNotEmpty(Collection<?> coll) {
		return !isEmpty(coll);
	}

	public static boolean isEmpty(Map map) {
		return (map == null || map.isEmpty());
	}

	public static boolean isNotEmpty(Map map) {
		return !isEmpty(map);
	}

	public static List subList(List list, int begin, int end) {
		if (isEmpty(list)) {
			return null;
		}
		begin = begin < 0 ? 0 : begin;
		end = end > list.size() ? list.size() : end;

		return list.subList(begin, end);
	}

	public static <T> List<T> defaultIfNull(List<T> list) {
		return list == null ? new ArrayList<T>() : list;
	}

	public static <T> T get(List<T> list, int location) {
		return isEmpty(list) || location < 0 || location >= list.size() ? null
				: list.get(location);
	}

	public static <T> T[] toArray(Collection<T> coll, Class<T> clazz) {
		if (isEmpty(coll)) {
			return null;
		}
		T[] ts = (T[]) Array.newInstance(clazz, coll.size());
		return coll.toArray(ts);

	}
}
